package com.ricky.handler.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.ricky.core.net.abs.Request;
import com.ricky.core.net.msg.Context;
import com.ricky.core.net.utils.Attributes;
import com.ricky.game.GameWorld;
import com.ricky.game.Hero;
import com.ricky.game.MsgComressionHelper;
import com.ricky.game.MsgHelper;
import com.ricky.game.Utils;
import com.ricky.game.scene.RoomController;
import com.ricky.handler.Default;
import com.ricky.handler.IHandler;

/**
 * 全局操作
 * @author rik
 *
 */
public class Pub implements IHandler {

	protected static Logger log = Logger.getLogger(Pub.class);

	@SuppressWarnings("unchecked")
	@Override
	public void proccess(Request request, Map<String,Object> context) {
		Hero h = (Hero) request.getAttribute(Attributes.HERO);
		@SuppressWarnings("rawtypes")
		Map msg =context;
		int type = (Integer) msg.get("t");
		switch (type) {
			case 0: //ping 
				Default.ping(request); 
				h.setAttribute("delay", msg.get("delay"));
			break;
			
			case 5://定向广播
				String HeroName=(String) msg.get("name");
				Map<String, Object> arg=(Map<String, Object>)msg.get("arg");
				sendMsgToHero(h,HeroName,arg);
			break;
			
			case 10://得到房间列表
				getRoomList(h);
				break;
				
		}
			
	}
	
	private void sendMsgToHero(Hero h,String toHero, Map<String,Object> info)
	{
//		GameWorld.getInstance().g
//		Hero to=
		MsgHelper.sendMsg(toHero,info);
	}
	
	
	
	/**
	 * 得到房间列表
	 * @param h
	 */
	public static void  getRoomList(Hero h)
	{
		Collection<RoomController> rooms=GameWorld.getInstance().getRoom().values();
		Map<String,Object> msg=new HashMap<String, Object>();
		List<Object>  roomlist=new ArrayList<Object> ();
		for(RoomController rc:rooms)
		{
			if(!rc.isLocked())
			{
				Object info=MsgComressionHelper.roomInfo(rc);
				roomlist.add(info);
			}
		}
		
		msg.put("cmd", "r.roomList");
		msg.put("arg", Utils.changeListToArray(roomlist));
		MsgHelper.sendMsg(h.getRequest(), msg);
	}
	
	
	
}
